import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('../views/Login')
const AdminHome = () => import('../views/admin/Home')
const AdminStudent = () => import('../views/admin/childrenCom/Student')
const AdminTeacher = () => import('../views/admin/childrenCom/Teacher')
const AdminNotice = () => import('../views/admin/childrenCom/Notice')
const AdminClasses = () => import('../views/admin/childrenCom/Classes')


const StudentHome = () => import('../views/student/Home')
const StudentNotice = () => import('../views/student/childrenCom/Notice')
const StudentHealth = () => import('../views/student/childrenCom/Health')
const StudentLeave = () => import('../views/student/childrenCom/Leave')
const StudentProfile = () => import('../views/student/childrenCom/Profile')


const TeacherHome = () => import('../views/teacher/Home')
const TeacherNotice = () => import('../views/teacher/childrenCom/Notice')
const TeacherHealth = () => import('../views/teacher/childrenCom/Health')
const TeacherLeave = () => import('../views/teacher/childrenCom/Leave')
const TeacherProfile = () => import('../views/teacher/childrenCom/Profile')
const Detail = () => import('../views/teacher/childrenCom/Detail')

Vue.use(VueRouter)
// 解决重复点击路由调整报红
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [{
    path: "/",
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/admin',
    component: AdminHome,
    children: [{
        path: "/",
        redirect: '/admin/student'
      },
      {
        path: '/admin/student',
        component: AdminStudent
      },
      {
        path: '/admin/teacher',
        component: AdminTeacher
      },
      {
        path: '/admin/notice',
        component: AdminNotice
      },
      {
        path: '/admin/classes',
        component: AdminClasses
      },

    ]
  },
  {
    path: '/student',
    component: StudentHome,
    children: [{
        path: '/',
        redirect: '/student/notice'
      },
      {
        path: '/student/notice',
        component: StudentNotice
      },
      {
        path: '/student/health',
        component: StudentHealth
      },
      {
        path: '/student/leave',
        component: StudentLeave
      },
      {
        path: '/student/Profile',
        component: StudentProfile
      }
    ]
  },
  {
    path: '/teacher',
    component: TeacherHome,
    children: [{
        path: '/',
        redirect: '/teacher/health'
      }, {
        path: '/teacher/notice',
        component: TeacherNotice
      },
      {
        path: '/teacher/health',
        component: TeacherHealth
      },
      {
        path: '/teacher/leave',
        component: TeacherLeave
      },
      {
        path: '/teacher/profile',
        component: TeacherProfile
      },
      {
        path: '/teacher/detail/:mod',
        component: Detail,
      }

    ]
  },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})


// 路由导航守卫 拦截不带token的访问
router.beforeEach((to, from, next) => {
  console.log(to.path);
  if (to.path === '/login') return next();
  const token = window.sessionStorage.getItem('token');
  if (!token) return next('/login');
  next()
})


export default router